<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;

class Bases extends Controller
{

    public function page($page ,$size,$success,$message){
        $return = [
            'data' => '',
            'success' => $success,
            'messages' => $message,
            'page' =>$page,
            'size' => $size
        ];
        $data2 = model('Bases')->order('update_time desc')->paginate($size, false, [
            'page' => $page,
            'var_page' => 'page'
        ]);
        $data = $data2->toArray();

        if ($page>$data['last_page']) {
            $data = model('Bases')->order('update_time desc')->paginate($size, false, [
                'page' => $data['last_page'],
                'var_page' => 'page'
            ]);
            $data = $data->toArray();
            $return['page'] = $data['last_page'];
            }
        $bases =  $data['data'];
        foreach ($bases as $k=>$v){
            $bases[$k]['file'] =json_decode($v['file']);
            $bases[$k]['tag']= explode(",",$bases[$k]['tag']);
             }
        $return['data'] = $bases;
        $return['total'] = $data['total'];

        $return['page'] = $data['current_page'];
        return $return;
        }
    //添加基地
    public function addBases(){
        $page = input('post.page');
        $size = input('post.size');


        $tag = input('post.tag');
        $tags ='';
        foreach ($tag as $v){
            $tags =$tags.",".$v;
        }
            $data = [
                'title' => input('post.title'),
                'tag' => $tags,
                'logo' => input('post.logo'),
                'author' => session('username'),
                'defaulePageview' => input('post.defaulePageview'),
                'address' => input('post.address'),
                'phone' => input('post.phone'),
                'file' => json_encode(input('post.file')),
                'content' => input('post.content'),
                'weight' => 0
            ];
            $resault = model('Bases')->addBases($data);
            if ($resault==1) {
                $return = $this->page($page,$size,true,"查询成功");

            }
            else{
                $return = $this->page($page,$size,false,$resault);
            }

            return json($return);
    }
    //删除基地
    public function deleteBases(){
        $page = input('delete.page');
        $size = input('delete.size');

        $id = input('delete.id');

        $basesInfo = model('Bases')->where('id',$id)->find();
        if (!$basesInfo){
            $return = $this->page($page,$size,false,'查无此基地');
        }else{
            $result = model('Bases')->where('id',$id)->delete();
            if ($result==1){
                $return = $this->page($page,$size,true,"删除成功");
            }else{
                $return = $this->page($page,$size,false,'删除失败');
            }
        }

        return json($return);

    }
    //基地列表
   public function selectAllBases(){
       $size = input('get.size');
       $page = input('get.page');
       $return = $this->page($page,$size,true,'查无成功');

       return json($return);
   }
    //基地地图
    public function basesMap(){

       $result = Db::table('baseMap')->where('id',1)->find();
      return json($result);
    }
     //更新基地图
    public function chengeMap(){
        $return = [

            'success' => false,
            'message' => "修改失败",

        ];
       $address =input('post.address');
        $result = Db::table('baseMap')->where('id',1)->update(['address'=>$address]);
        if ($result){
            $return['message'] ="修改成功";
            $return['success'] =true;
        }
        return json($return);
    }
    public function updateBases(){
        $size = input('put.size');
        $page = input('put.page');

        $tag = input('post.tag');
        $tags ='';
        foreach ($tag as $v){
            $tags =$tags.",".$v;
        }
            $data = [
                'title' => input('put.title'),
                'tag' => $tags,
                'logo' => input('post.logo'),
                'author' => session('username'),
                'defaulePageview' => input('post.defaulePageview'),
                'address' => input('post.address'),
                'phone' => input('post.phone'),
                'file' => json_encode(input('post.file')),
                'content' => input('post.content'),
                'update_time' =>time()


            ];
            $id = input('put.id');
            $basesInfo =model('Bases')->where('id',$id)->find();
            if ($basesInfo) {
                $result = model('Bases')->updateBases($id, $data);
                if ($result) {
                    $return = $this->page($page, $size, true, '更新成功');
                } else {
                    $return = $this->page($page, $size, false, '更新失败');
                }
            }else{
                $return = $this->page($page, $size, false, '查询不到该基地');
            }
        return json($return);
    }
    //修改权重
    public function weight()
    {
        $id = input('put.id');
        $size = input('put.size');
        $page = input('put.page');
        $weight = input('put.weight');

        $result = model('Bases')->where('id', $id)->update(['weight' => $weight]);

        if ($result) {
            $return = $this->page($page,$size,true,'更新成功');
        }else{
            $return = $this->page($page,$size,false,'更新失败');
        }

        return json($return);
    }
    //基地详情
    public function contentBases(){
        $json = [
            'data' => '',
            'success' => false,
            'messages' => '查询基地失败'
        ];

        $id = input('get.id');

        $ArticleInfo = model('Bases')->where('id',$id)->find();

        if ($ArticleInfo){

            $ArticleInfo['file'] =json_decode($ArticleInfo['file']);
            $ArticleInfo['tag'] = explode(",",$ArticleInfo['tag']);
            $json['data'] = $ArticleInfo;
            $json['success'] = true;
            $json['messages'] ='查询成功';


        }
        return json($json);
    }

}
